package entity

import (
	"errors"
	"time"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

// COMP_JC_DR 托管中收导入
type COMP_JC_DR struct {
	ID     int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	JCRWMC string    `gorm:"column:JCRWMC"`
	SSTX   string    `gorm:"column:SSTX"`
	SYCRQ  string    `gorm:"column:SYCRQ"`
	JYDW   string    `gorm:"column:JYDW"`
	FZR    string    `gorm:"column:FZR"`
	KHJL1  string    `gorm:"column:KHJL1"`
	KHJL2  string    `gorm:"column:KHJL2"`
	KHJL3  string    `gorm:"column:KHJL3"`
	KHJL4  string    `gorm:"column:KHJL4"`
	KHJL5  string    `gorm:"column:KHJL5"`
	KHMC   string    `gorm:"column:KHMC"`
	GLKHMC string    `gorm:"column:GLKHMC"`
	ZH     string    `gorm:"column:ZH"`
	JCLXID int       `gorm:"column:JCLXID"`
	JCLX   string    `gorm:"column:JCLX"`
	JCQSR  time.Time `gorm:"column:JCQSR"`
	JCJZR  time.Time `gorm:"column:JCJZR"`
	CRJE   float32   `gorm:"column:CRJE"`
	CRSQLL string    `gorm:"column:CRSQLL"`
	BZ     string    `gorm:"column:BZ"`
	RWXH   string    `gorm:"column:RWXH"`
	DRJG   string    `gorm:"column:DRJG"`
}

// TableName 经营单位中收导入表
func (COMP_JC_DR) TableName() string {
	return "T_COMP_JC_DR"
}

// BeforeSave 保存前的触发器
func (i *COMP_JC_DR) BeforeSave(tx *gorm.DB) (err error) {
	if i.JCLX == "一般贷款日均" {
		i.JCLXID = 1
	} else if i.JCLX == "贴现日均" {
		i.JCLXID = 2
	} else if i.JCLX == "福费廷日均" {
		i.JCLXID = 3
	} else if i.JCLX == "存款日均" {
		i.JCLXID = 4
	} else if i.JCLX == "存款利润" {
		i.JCLXID = 5
	} else if i.JCLX == "中收" {
		i.JCLXID = 6
	} else {
		return errors.New("无法识别的监测类型，请重新导入【监测类型：一般贷款日均、贴现日均、福费廷日均、存款日均、存款利润、中收】")
	}
	return nil
}

// COMP_JC_DRAfterImport 导入后执行
func COMP_JC_DRAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	tx.Exec(`UPDATE T_COMP_JC_DR SET RWXH='R-'+CAST(B.ID AS nvarchar(10))
	FROM T_COMP_JC_DR A INNER JOIN (SELECT JCRWMC,MIN(ID) ID FROM T_COMP_JC_DR GROUP BY JCRWMC) B ON A.JCRWMC=B.JCRWMC`)
	return nil
}

// COMP_JC_DREF 返回结构体
func COMP_JC_DREF() base.EntityFunc {
	return func() interface{} {
		entity := COMP_JC_DR{}
		return &entity
	}
}

// COMP_JC_DRESF 返回结构体数组
func COMP_JC_DRESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []COMP_JC_DR
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("COMP_JC_DR", base.NewDefaultDao(COMP_JC_DREF(), COMP_JC_DRESF(), nil))
	base.SetAfterCallBack("COMP_JC_DR", COMP_JC_DRAfterImport)
}
